package com.stenio.fancat.service.wx;

import com.stenio.fancat.common.errorCode.ErrorCode;
import com.stenio.fancat.domain.user.UserRelationshipFomId;
import com.stenio.fancat.exception.FanCatException;
import com.stenio.fancat.mapper.user.UserRelationshipFomIdMapper;
import com.stenio.fancat.util.EmptyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class GetFormIdService {

    @Autowired
    private UserRelationshipFomIdMapper userRelationshipFomIdMapper;

    /**
     * 获取user的formId
     * @param list
     * @param userId
     * @return
     * @throws FanCatException
     */
    @Transactional
    public Integer getFormId(List<String> list, String userId) throws FanCatException {
        if(EmptyUtil.isEmpty(list) ||EmptyUtil.isEmpty(userId)){
            throw new FanCatException("参数异常", ErrorCode.parameters);
        }

        List<UserRelationshipFomId> fomIdList = new ArrayList<>();
        Date date = new Date();
        for(int i=0;i<list.size();i++){
            UserRelationshipFomId userRelationshipFomId = new UserRelationshipFomId();
            userRelationshipFomId.setFormId(list.get(i));
            userRelationshipFomId.setCreateTime(date);
            userRelationshipFomId.setExpireTime(date);
            userRelationshipFomId.setStatus(0);
            userRelationshipFomId.setUserId(userId);
            fomIdList.add(userRelationshipFomId);
        }
        Integer number = 0;

        number = userRelationshipFomIdMapper.insertFormIdByUid(fomIdList);

        return number;
    }

}
